<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>杂费信息管理</span>
      </div>
    </template>
    <el-row class="container" v-for="(item, index) in attrTypeList" :key="index">
      <el-col :span="2" class="text-center">
        <el-popconfirm
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteFeeHandle(item)"
          @cancel="editFeeHandle(item)"
          :title="`修改或删除${item.name}`"
        >
          <template #reference>
            <div class="item m-r-10 pointer">
              <span>{{ item.name }}</span>
              <el-tooltip effect="dark" content="点击属性名可进行修改/删除操作" placement="top-start">
                <el-icon class="m-l-1"><InfoFilled /></el-icon>
              </el-tooltip>
            </div>
          </template>
        </el-popconfirm>
      </el-col>
      <el-col :span="22" class="item-container">
        <el-popconfirm
          v-for="item2 in item.feeValueList || []"
          :key="item2.id"
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteFeeValueHandle(item2)"
          @cancel="editFeeValueHandle(item2)"
          :title="`修改或删除${item2.name}`"
        >
          <template #reference>
            <div class="item m-r-5 pointer">
              <el-tag style="padding: 13px">
                {{ item2.name + item2.unit }}
              </el-tag>
            </div>
          </template>
        </el-popconfirm>
        <el-icon class="item m-r-5 pointer" :size="35" color="#409EFF" @click="addFeeValueHandle(item)">
          <CirclePlus />
        </el-icon>
      </el-col>
    </el-row>
    <el-button type="primary" class="m-t-5" @click="addFeeHandle"> 添加新属性 </el-button>
    <!--信息修改弹窗管理-->
    <el-dialog v-model="feeKeyDialog.visible" :destroy-on-close="true" :title="feeKeyDialog.title" style="max-width: 700px">
      <el-form ref="feeKeyFormRef" :model="feeKeyData.form" :rules="feeKeyData.rules" label-width="120px">
        <el-form-item label="属性名称" prop="name">
          <el-input v-model.trim="feeKeyData.form.name" autocomplete="off" placeholder="请输入属性名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="feeKeySubmitForm">确 定</el-button>
          <el-button @click="feeKeyCancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 属性信息弹窗管理-->
    <el-dialog v-model="feeValueDialog.visible" :destroy-on-close="true" :title="feeValueDialog.title" style="max-width: 700px">
      <el-form ref="feeValueFormRef" :model="feeValueData.form" :rules="feeValueData.rules" label-width="120px">
        <el-form-item label="属性值名称" prop="name">
          <el-input v-model.trim="feeValueData.form.name" autocomplete="off" placeholder="请输入属性值名称" />
        </el-form-item>
        <el-form-item label="属性值单位" prop="unit">
          <el-input v-model.trim="feeValueData.form.unit" autocomplete="off" placeholder="请输入，例：元/月" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="feeValueSubmitForm">确 定</el-button>
          <el-button @click="feeValueCancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import { FeeKeyVO, FeeKeyForm, FeeKeyQuery } from '@/api/apartment/attribute/feeKey/types';
import { FeeValueVO, FeeValueForm, FeeValueQuery } from '@/api/apartment/attribute/feeValue/types';
import { delFeeKey, listFeeKey, updateFeeKey, addFeeKey, getFeeKey } from '@/api/apartment/attribute/feeKey/index';
import { delFeeValue, getFeeValue, updateFeeValue, addFeeValue } from '@/api/apartment/attribute/feeValue/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const buttonLoading = ref(false);
const feeKeyFormRef = ref<ElFormInstance>();
const feeValueFormRef = ref<ElFormInstance>();
const feeKeyDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const feeValueDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFeeKeyFormData: FeeKeyForm = {
  id: undefined,
  name: undefined
};
const feeKeyData = reactive<PageData<FeeKeyForm, FeeKeyQuery>>({
  form: { ...initFeeKeyFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 500,
    name: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '属性名称不能为空', trigger: 'blur' }]
  }
});
const initFeeValueFormData: FeeValueForm = {
  id: undefined,
  name: undefined,
  feeKeyId: undefined,
  feeKeyName: undefined
};
const feeValueData = reactive<PageData<FeeValueForm, FeeValueQuery>>({
  form: { ...initFeeValueFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    feeKeyId: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '属性值不能为空', trigger: 'blur' }],
    unit: [{ required: true, message: '属性值单位不能为空', trigger: 'blur' }]
  }
});
// 属性类型
const attrTypeList = ref<FeeKeyVO[]>([]);
// 获取属性信息
const getFeeKeyInfoListHandle = async () => {
  const { queryParams, form, rules } = toRefs(feeKeyData);
  const { data } = await listFeeKey(queryParams.value);
  data.forEach((item) => {
    item.feeValueList?.forEach((item2) => {
      item2.feeKeyName = item.name;
    });
  });
  attrTypeList.value = data;
};
// 删除属性值
const deleteFeeValueHandle = async (item: FeeValueVO) => {
  await delFeeValue(item.id);
  await getFeeKeyInfoListHandle();
  proxy?.$modal.msgSuccess('操作成功');
};
// 编辑属性值
const editFeeValueHandle = async (item: FeeValueVO) => {
  const { form } = toRefs(feeValueData);
  feeValueReset();
  const _id = item?.id;
  const res = await getFeeValue(_id);
  Object.assign(form.value, res.data);
  feeValueDialog.visible = true;
  feeValueDialog.title = '修改房间' + item.name + '属性值';
};
// 添加属性值
const addFeeValueHandle = (item: FeeKeyVO) => {
  feeValueReset();
  feeValueDialog.visible = true;
  feeValueDialog.title = '添加房间' + item.name + '属性值';
  const { form } = toRefs(feeValueData);
  form.value.feeKeyId = item.id;
  form.value.feeKeyName = item.name;
};
// 添加新属性
const addFeeHandle = () => {
  feeKeyReset();
  feeKeyDialog.visible = true;
  feeKeyDialog.title = '添加房间基本属性';
};
// 删除属性
const deleteFeeHandle = async (item: FeeKeyVO) => {
  await delFeeKey(item.id);
  await getFeeKeyInfoListHandle();
  proxy?.$modal.msgSuccess('操作成功');
};
// 编辑属性
const editFeeHandle = async (item: FeeKeyVO) => {
  const { form } = toRefs(feeKeyData);
  feeKeyReset();
  const _id = item?.id;
  const res = await getFeeKey(_id);
  Object.assign(form.value, res.data);
  feeKeyDialog.visible = true;
  feeKeyDialog.title = '修改房间基本属性';
};
/** 提交按钮 */
const feeKeySubmitForm = () => {
  const { queryParams, form, rules } = toRefs(feeKeyData);
  feeKeyFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateFeeKey(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addFeeKey(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      feeKeyDialog.visible = false;
      await getFeeKeyInfoListHandle();
    }
  });
};
/** 取消按钮 */
const feeKeyCancel = () => {
  feeKeyReset();
  feeKeyDialog.visible = false;
};
/** 表单重置 */
const feeKeyReset = () => {
  const { form } = toRefs(feeKeyData);
  form.value = { ...initFeeKeyFormData };
  feeKeyFormRef.value?.resetFields();
};

/** 提交按钮 */
const feeValueSubmitForm = () => {
  const { queryParams, form, rules } = toRefs(feeValueData);
  feeValueFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateFeeValue(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addFeeValue(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      feeValueDialog.visible = false;
      await getFeeKeyInfoListHandle();
    }
  });
};
/** 取消按钮 */
const feeValueCancel = () => {
  feeValueReset();
  feeValueDialog.visible = false;
};
/** 表单重置 */
const feeValueReset = () => {
  const { form } = toRefs(feeValueData);
  form.value = { ...initFeeValueFormData };
  feeValueFormRef.value?.resetFields();
};

onMounted(() => {
  getFeeKeyInfoListHandle();
});
</script>

<style scoped lang="scss">
.card-header {
  font-size: 18px;
  font-weight: bold;
}

.text-center {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
}

.container:not(:first-child) {
  margin-top: 20px;
}

.item-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 10px 15px;
  background-color: #efefef;
  border-radius: 20px;

  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
}
</style>
